package com.roc.base.mall.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtilParent {
	public final static String YYYY_MM_DD = "yyyy-MM-dd";

	public final static String YYYYMMDD = "yyyyMMdd";

	public final static String YYMMDDHHMMSS = "yyMMddHHmmss";

	public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public final static String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static List<String> allBetweenDate(String startDate, String endDate) {
		List<String> allDate = new ArrayList<String>();
		SimpleDateFormat sfmt = new SimpleDateFormat(YYYY_MM_DD);
		try {
			Date dateStart = sfmt.parse(startDate);
			Date dateEnd = sfmt.parse(endDate);
			Calendar calStart = Calendar.getInstance();
			calStart.setTime(dateStart);
			Calendar calEnd = Calendar.getInstance();
			calEnd.setTime(dateEnd);
			calEnd.add(Calendar.DATE, 1);
			String date = "";
			while (!calStart.equals(calEnd)) {
				date = "";
				date = calStart.get(Calendar.YEAR) + "-";
				if ((calStart.get(Calendar.MONTH) + 1) < 10) {
					date += "0";
				}
				date += (calStart.get(Calendar.MONTH) + 1) + "-";
				if (calStart.get(Calendar.DAY_OF_MONTH) < 10) {
					date += "0";
				}
				date += calStart.get(Calendar.DAY_OF_MONTH);
				allDate.add(date);
				calStart.add(Calendar.DATE, 1);
			}
		} catch (ParseException e) {
		}
		return allDate;
	}

	/**
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date strToDate(String dateStr, String pattern) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		try {
			Date datetime = dateFormat.parse(dateStr);
			Calendar cal = Calendar.getInstance();
			cal.setTime(datetime);

			return cal.getTime();

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateToStr(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 */
	public static Date getCurrentDate() {
		return Calendar.getInstance().getTime();
	}

	/**
	 */
	public static Date getTime(int calendarType, int value) {
		Calendar calDay = Calendar.getInstance();
		calDay.add(calendarType, value);
		return calDay.getTime();
	}

	/**
	 */
	public static Date getTime(Date time, int calendarType, int value) {
		Calendar calDay = Calendar.getInstance();
		calDay.setTime(time);
		calDay.add(calendarType, value);
		return calDay.getTime();
	}

	/**
	 */
	public static String getCurrentDate(String formatType) {
		Date now = Calendar.getInstance().getTime();
		return dateToStr(now, formatType);
	}

	/**
	 */
	public static long minutesBetween(Date date1, Date date2) {

		long date1Time = 0;
		long date2Time = 0;
		if (date1 != null) {
			date1Time = date1.getTime();
		}
		if (date2 != null) {
			date2Time = date2.getTime();
		}
		return (long) ((date2Time - date1Time) / (1000 * 60));
	}

	/**
	 * @param date
	 * @return
	 */
	public static String dateToYYYMMDD(Date date) {
		return dateToStr(date, YYYYMMDD);
	}

	/**
	 * @param date
	 * @return
	 */
	public static String dateToYYYYMMDDHHMMSS(Date date) {
		return dateToStr(date, YYYYMMDDHHMMSS);
	}
}
